﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            CustomerRepo _CustomerRepo = new CustomerRepo();
            IEnumerable<CustomerModels> Customers = _CustomerRepo.GetCustomer();
            return View(Customers);
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            CustomerRepo _CustomerRepo = new CustomerRepo();
            CustomerModels Customer = new CustomerModels();
            Customer = _CustomerRepo.GetCustomerById(id);
            return View(Customer);
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                CustomerRepo _CustomerRepo = new CustomerRepo();
                CustomerModels Customer = new CustomerModels();

                Customer.CustNameTh = collection["CustNameTh"];
                Customer.CustNameEng = collection["CustNameEng"];
                Customer.AddressTh = collection["AddressTh"];
                Customer.AddressEng = collection["AddressEng"];
                Customer.Tel = collection["Tel"];
                Customer.CorporateName = collection["CorporateName"];
                Customer.CreateBy = (string)Session["User"];

                _CustomerRepo.Create(Customer); 

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Customer/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            CustomerRepo _CustomerRepo = new CustomerRepo();
            CustomerModels Customer = new CustomerModels();
            Customer = _CustomerRepo.GetCustomerById(id);
            return View(Customer);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                CustomerRepo _CustomerRepo = new CustomerRepo();
                CustomerModels Customer = new CustomerModels();
                Customer.CustId = id;
                Customer.CustNameTh = collection["CustNameTh"];
                Customer.CustNameEng = collection["CustNameEng"];
                Customer.AddressTh = collection["AddressTh"];
                Customer.AddressEng = collection["AddressEng"];
                Customer.Tel = collection["Tel"];
                Customer.CorporateName = collection["CorporateName"];
                Customer.UpdateBy = (string)Session["User"];


                bool result = _CustomerRepo.Edit(Customer);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Customer/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            CustomerRepo _CustomerRepo = new CustomerRepo();
            CustomerModels Customer = new CustomerModels();
            Customer = _CustomerRepo.GetCustomerById(id);
            return View(Customer);
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                CustomerRepo _CustomerRepo = new CustomerRepo();
                CustomerModels Customer = new CustomerModels();
                Customer.CustId = id;
                Customer.UpdateBy = (string)Session["User"];
                bool result = _CustomerRepo.Delete(Customer);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
